<?php

use Doraemon\tools\Log;
use Doraemon\model\Photos;
use Doraemon\tools\IdParse;
use Doraemon\service\Article;
use Doraemon\tools\PointConfig;
use Doraemon\cache\ContentAudit;
use Doraemon\amqp\CommunityQueue;
use Biz\model\wiki\ObjRelateWiki;
use Doraemon\cache\UserNewPhotoRedis;
use Doraemon\cache\UserHotPhotoRedis;
use Biz\module\Classes\wiki\RelateWiki;
use Doraemon\service\Photo\MachineMark as sMachineMark;
use Doraemon\service\DesignerInfoLib\OperateInfoService as sOperateInfo;

class PublishArticleAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * 迁移hhz_home队列 - 发布整屋
     *
     * @throws Exception
     */
    public function main()
    {
        Log::info('publishArticle操作接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['database']) {
            throw new \Exception('未获取到数据库');
        }
        if (!$this->messageBody['table']) {
            throw new \Exception('未获取到数据表');
        }
        if (!$this->messageBody['data']) {
            throw new \Exception('未接收到数据');
        }

        $sArticleId = $this->messageBody['data']['aid'];
        $iUid = $this->messageBody['data']['uid'];

        $aPhotoIds = $this->getPhotoIdList($sArticleId);

        //增加积分
        $this->addScore($sArticleId);
        //设置note为待审核
        $this->setPhotoToWaitAudit($iUid, $sArticleId, $aPhotoIds);
        //设置wiki数量
        $this->setWikisNumByObjId($sArticleId);
        //删除用户最新图片缓存
        UserNewPhotoRedis::delItem($iUid);
        UserHotPhotoRedis::delItem($iUid);

//        \Doraemon\service\UserFlowFormat\UserFlowFormatHelper::debugDing('debug成功：' . json_encode($this->messageBody));
        // 更新设计师信息库中的整屋优质率
        sOperateInfo::updateArticleHighRate($iUid);
        $this->setSuccess();
    }

    /**
     * 增加积分
     *
     * @param $article_id
     * @return void
     * @time 2022/11/16 10:41
     * @desc
     */
    public function addScore($article_id): void
    {
        //积分操作-发布整屋增加积分
        CommunityQueue::insertQueue(PointConfig::MQ_QUEUE_URL, [
            'event_name' => PointConfig::POINT_TYPE_ARTICLE,
            'obj_id' => $article_id,
            'uid' => IdParse::getUid($article_id),
            'type' => PointConfig::POINT_AWARD
        ], CommunityQueue::QUEUE_COMMUNITY);

    }

    /**
     * 设置关联wiki数量
     *
     * @param $article_id
     * @return void
     * @time 2022/11/16 10:49
     * @desc
     */
    public function setWikisNumByObjId($article_id): void
    {
        RelateWiki::setWikisNumByObjId($article_id, ObjRelateWiki::OBJ_TYPE_ARTICLE);
    }

    /**
     * 获取noteId集合
     *
     * @param $article_id
     * @return array
     * @time 2022/11/16 11:15
     * @desc
     */
    public function getPhotoIdList($article_id): array
    {
        $aPhotoIds = [];
        $aArticleSpaceData = Article::getArticleSpaceInfoById($article_id);
        if ($aArticleSpaceData) {
            foreach ($aArticleSpaceData as $aInfo) {
                $aShowPics = json_decode($aInfo['show_pics'], true);
                $aTempPhotoIds = array_column($aShowPics, 'photo_id');
                $aPhotoIds = array_merge($aTempPhotoIds, $aPhotoIds);
            }
        }
        return $aPhotoIds;
    }

    /**
     * 将图片置为待审核
     *
     * @param $iUid
     * @param $sArticleId
     * @param $aPhotoIds
     * @return bool
     * @time 2022/11/16 11:44
     * @desc
     */
    public function setPhotoToWaitAudit($iUid, $sArticleId, $aPhotoIds): bool
    {
        $aList = Photos::getPhotosByUidAid($iUid, $sArticleId);
        $aArticlePhotoStatuses = [];
        $aArticlePhotoAlbum = [];
        if (!$aList) {
            return false;
        }
        foreach ($aList as $article) {
            $aArticlePhotoStatuses[$article['id']] = $article['status'];
            $aArticlePhotoAlbum[$article['id']] = $article['is_album'];
        }

        foreach ($aPhotoIds as $sPhotoId) {
            if (isset($aArticlePhotoStatuses[$sPhotoId]) && (int)$aArticlePhotoStatuses[$sPhotoId] === 3) {
                //图片审核
                if (isset($aArticlePhotoAlbum[$sPhotoId]) && $aArticlePhotoAlbum[$sPhotoId]) {
                    ContentAudit::setAuditIdBykey(ContentAudit::AUDIT_NOTE, $sPhotoId, time());
                } else {
                    ContentAudit::setAuditIdBykey(ContentAudit::AUDIT_PHOTO, $sPhotoId, time());
                }
                Photos::updatePhotoById($sPhotoId, ['status' => Photos::STATUS_UNTREATED, 'addtime' => time()]);
            }
            Log::info('publishArticle的Note自动审核', [$sPhotoId, $iUid, $sArticleId]);
            sMachineMark::autoScore($sPhotoId);
        }
        return true;
    }
}
